package threadPool;


import java.util.concurrent.*;

public class ThreadPoolDemo {
    public static void main(String[] args) {
//        ExecutorService threadPool = Executors.newFixedThreadPool(5); // 一池5个线程
//        ExecutorService threadPool = Executors.newSingleThreadExecutor(); // 一个池1个线程，一个任务一个任务地执行
//        ExecutorService threadPool = Executors.newCachedThreadPool();   // 可伸缩的线程池
//        ForkJoinTask
        ExecutorService threadPool = new ThreadPoolExecutor(2,
                Runtime.getRuntime().availableProcessors() + 1,
                2L,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(3),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.DiscardOldestPolicy());
        try {
            for (int i = 1; i < 20; i++) {
                final int tmpI = i;
                threadPool.execute(() -> {
                    System.out.println(Thread.currentThread().getName() + "\t 办理业务" + tmpI);
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            threadPool.shutdown();
        }
        threadPool.shutdown();
    }
}
